/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.language;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import ac.jp.u_tokyo.SyncLib.SyncFactory;

public class Program
{

	public final static String CONST_PREFIX = "_const_";
	
	private Map<String, Const> _valueMap = new HashMap<String, Const>();
	
	public void addConstDecl(String name, Const value) {
		_valueMap.put(name, value);
	}
	
	public Const getConst(String name) {
		return _valueMap.get(name);
	}
	
	private Map<String, SyncDecl> _syncDeclMap = new HashMap<String, SyncDecl>();
	
	public void addSyncDecl(String name, SyncDecl value) {
		_syncDeclMap.put(name, value);
	}
	
	public SyncDecl getSyncDecl(String name) {
		return _syncDeclMap.get(name);
	}
	
//	public void evaluate() throws EvaluationFailedException{
//		for(Const c : _valueMap.values()) {
//			c.evaluate(this);
//		}
//	}
	
	public void write(Appendable sb) throws IOException, EvaluationFailedException {
		VarNameGenerator g = new VarNameGenerator();
		for (SyncDecl s : _syncDeclMap.values()) {
			s.write(sb, g, new MapDelegation(_valueMap));
		}
	}
	
	static String getSyncFactoryFullName() {
		return SyncFactory.class.getName();
	}
	
	static String syncNameToMethodName(String syncName) {
		return "get" + syncName + "Factory";
	}
}
